package com.slife.serial.core;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import com.slife.serial.utils.ByteOrderUtils;
import com.slife.serial.utils.HexUtils;
import com.slife.serial.utils.IEEE754Utils;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static com.slife.serial.utils.HexUtils.byteToHex;

public class ByteOrderExample {



    public static void main(String[] args) {
        long value = 16909060;


//        byte[] bytes1 = ByteUtil.longToBytes(value);
//
//        String s1 = byteToHex(bytes1);
//        System.out.println(s1);
        String hex = HexUtil.toHex(value);

        System.out.println(hex);


        byte[] bytes = ByteUtil.floatToBytes(4.00f);
        String s = byteToHex(bytes);
        System.out.println(s);



//        byte[] bigEndianBytes = ByteOrderUtils.convertToBigEndian(value);
//        byte[] littleEndianBytes = ByteOrderUtils.convertToLittleEndian(value);
//
//        float floatValue = 4.00f;
//        byte[] bigEndianFloatBytes = ByteOrderUtils.convertToBigEndian(floatValue);
//        byte[] littleEndianFloatBytes = ByteOrderUtils.convertToLittleEndian(floatValue);
//：08 04 08 00 6C 00 00 00 7B 00 00 D6 8E
//        006C0000 007B0000
//        // 打印结果
//        System.out.print("Big Endian: ");
//        printBytes(bigEndianBytes);
//        System.out.print("Little Endian: ");
//        printBytes(littleEndianBytes);
//        System.out.print("Big Endian Float: ");
//        printBytes(bigEndianFloatBytes);
//        System.out.print("Little Endian Float: ");
//        printBytes(littleEndianFloatBytes);
    }

    private static void printBytes(byte[] bytes) {
        for (byte b : bytes) {
            System.out.printf("%02X ", b);
        }
        System.out.println();
    }

    public static void test1(){
        // 长整型16909060的大端字节顺序排列
        long value = 16909060L;
        byte[] bigEndianBytes = ByteBuffer.allocate(8)
                .order(ByteOrder.BIG_ENDIAN)
                .putLong(value)
                .array();
        byte[] bigEndianResult = new byte[4];
        System.arraycopy(bigEndianBytes, 4, bigEndianResult, 0, 4);
        System.out.print("Big Endian: ");
        printBytes(bigEndianResult);

        // 长整型16909060的小端字节顺序排列
        byte[] littleEndianBytes = ByteBuffer.allocate(8)
                .order(ByteOrder.LITTLE_ENDIAN)
                .putLong(value)
                .array();
        byte[] littleEndianResult = new byte[4];
        System.arraycopy(littleEndianBytes, 0, littleEndianResult, 0, 4);
        System.out.print("Little Endian: ");
        printBytes(littleEndianResult);

        // 浮点数4.00的大端字节顺序排列
        float floatValue = 4.00f;
        byte[] bigEndianFloatBytes = ByteBuffer.allocate(4)
                .order(ByteOrder.BIG_ENDIAN)
                .putFloat(floatValue)
                .array();
        System.out.print("Big Endian Float: ");
        printBytes(bigEndianFloatBytes);

        // 浮点数4.00的小端字节顺序排列
        byte[] littleEndianFloatBytes = ByteBuffer.allocate(4)
                .order(ByteOrder.LITTLE_ENDIAN)
                .putFloat(floatValue)
                .array();
        System.out.print("Little Endian Float: ");
        printBytes(littleEndianFloatBytes);
    }


}
